//查询按钮点击 请求患者池列表数据  start
$('#patientPoolSearchBtn').click(function(){
    var params=getSearchParams($(this));
    if(params){
        getpatientPoolList(params);
    }
});
//查询按钮点击 请求患者池列表数据  end

//患者池列表 获取数据 方法  start
function getpatientPoolList(result){
    ajaxGet(RESERVATIONCENTERURL.patientPool.doctor.index, result, function (data) {
        var is_reception={
            false:'否',
            true:'是'
        };
        if (data.code === 10000) {
            var str='';
            if($('#side-menu strong.font-bold').attr('data-doctor')){
                $.each(data.data,function(i,v){
                    str+='<tr data-reservationId="'+v.id+'"><td><lable>' +
                        '<input type="checkbox" class="checkBox">'+ (i+1) +'</lable></td>' +
                        '<td>'+v.name+'</td><td>'+v.sex.value+'</td><td>'+v.age+'</td>' +
                        '<td>'+v.phone+'</td><td>'+v.reservation_date+' '+v.reservation_time.reservation_start+'-'+v.reservation_time.reservation_end+'</td>' +
                        '<td>'+is_reception[v.is_reception]+'</td><td>' +
                        '<button type="button" class="btn btn-warning btn-outline margin-right-5 viewCase" data-patient-id="'+data.data[i].patient_id+'">查看病历</button>' +
                        '<button type="button" class="btn btn-danger btn-outline margin-right-5 refused">拒绝</button>' +
                        '<button type="button" class="btn btn-primary btn-outline margin-right-5 accept">接受</button></td></tr>';
                });
            }else{
                $.each(data.data,function(i,v){
                    str+='<tr data-reservationId="'+v.id+'" data-reservationTime="'+v.reservation_date+'"><td><lable>' +
                        '<input type="checkbox" class="checkBox">'+ (i+1) +'</lable></td>' +
                        '<td>'+v.name+'</td><td>'+v.sex.value+'</td><td>'+v.age+'</td><td>'+v.phone+'</td>' +
                        '<td>'+v.reservation_date+' '+v.reservation_time.reservation_start+'-'+v.reservation_time.reservation_end+'</td>' +
                        '<td><button type="button" class="btn btn-primary btn-outline margin-right-5 allotment">分配</button></td></tr>';
                });
            }
            $('#patientPoolListBody').html(str);
            // 页码
            if(data.meta.pagination.total_pages>1){
                page('patientPoolPager',data.meta,getpatientPoolList,result);
            }else{
                $('#patientPoolPager').html('');
            }
            $('#reservationCenterNav li[data-page="patientPool"]').attr('data-load','yes');//改变标签栏状态
        }else if(data.code === 40100){
            aClick();
        }else if(data.code === '40001'){
            $('#patientPoolListBody').html('<tr><td colspan="8">暂无数据</td></tr>');
            $('#patientPoolPager').html('');
        }else{
            alertModel('','获取失败，请重试！');
        }
    }, function (x,t,e) { })
};
//患者池列表 获取数据 方法  end

//查看病历按钮  点击 start
$('#patientPoolListBody').delegate('td button.viewCase','click',function(){
    var id = $(this).attr('data-patient-id');

    $.get('/manage/record/list/'+id, function (data) {
        var str = '';
        $.each(data, function (i, v) {
            str += '<option value="'+v.id+'">'+v.visit_type.value+v.clinic_at+'</option>';
        });

        $('#witch_record').html(str).change();
        $('#viewCaseModal').modal('show');
    }, 'json');
})

//分配  模态框 选择医生  请求能分配的医生放入框内  start
function assignedDoctor(){
    var reservationIdArr=$('#selectDistributionSaveBtn').attr('data-reservationIdArr').split(',');
    reservationIdArr=parseInt(reservationIdArr[0]);
    $.get(RESERVATIONCENTERURL.patientPool.admin.doctors,{id:reservationIdArr},function(data){
        if(data.code==10000){
            var str='';
            var status={
               0:{className:'stop',disabled:true},
               1:{className:'normal',disabled:false},
               2:{className:'full',disabled:false},
               3:{className:'detonation',disabled:true}
            };
            var V=data.data;
            for(var j=0;j<V.length;j++){
                str+='<label class="'+status[V[j].status.key].className+'">';
                if(status[V[j].status.key].disabled){
                    str+='<input type="radio" name="distributionName" value="'+V[j].doctor_id+'" disabled>';
                }else{
                    str+='<input type="radio" name="distributionName" value="'+V[j].doctor_id+'">';
                }
                str+='<span class="text" title="'+V[j].name+'">'+V[j].room+' '+V[j].name+'</span>' +
                    '<span class="status">'+V[j].status.value+'</span></label>';
            }
            $('#selectDistribution div.selectDoctorBox').html(str);
        }else{
            $('#selectDistributionSaveBtn').prev().click();  //关闭模态框
            alertModel('','获取可分配的医生获取失败！');
        }
    });
};
//分配  模态框 选择医生  请求能分配的医生放入框内  end
//管理员  一键随机分配  和选择分配  和  操作栏分配按钮   start
$('#randomlyAssignedBtn').click(function(){
    var reservationIdArr = getReservationId('patientPoolListBody');
    if (reservationIdArr.length) {
        confirmModel('','确定要一键随机分配吗？','','',function(){
            ajaxPost('', {ids:reservationIdArr}, function(data){
                if(data.code == 10000){
                    $('#patientPoolSearchBtn').click();
                    alertModel('','随机分配成功！');
                }else if(data.code === 40100){
                    aClick();
                }else {
                    alertModel('','随机分配失败，请重试！');
                }
            }, function(){ })
        });
    }else{
        alertModel('','请先选择患者！');
    }
});
$('#selectDistributionBtn').click(function(){
    var reservationIdArr = getReservationId('patientPoolListBody');
    if (reservationIdArr.length) {
        for(var i=1;i<reservationIdArr.length;i++){
            if($('#patientPoolListBody tr[data-reservationid="'+reservationIdArr[0]+'"]').attr('data-reservationTime') != $('#patientPoolListBody tr[data-reservationid="'+reservationIdArr[i]+'"]').attr('data-reservationTime')){
                alertModel('','批量分配只能选择预约时间为同一天！');
                return false;
            }
        }
        $('#selectDistribution').modal('show');  //打开修改诊室模态框
        $('#selectDistributionSaveBtn').attr('data-reservationIdArr',reservationIdArr);
        $('#selectDistributionSaveBtn').attr('data-tableId','patientPoolListBody');
        assignedDoctor();
    }else{
        alertModel('','请先选择患者！');
    }
});
$('#patientPoolListBody').on('click','.allotment',function(){
    var reservationIdArr=new Array($(this).parent().parent().attr('data-reservationid'));
    $('#selectDistribution').modal('show');  //打开修改诊室模态框
    $('#selectDistributionSaveBtn').attr('data-reservationIdArr',reservationIdArr);
    $('#selectDistributionSaveBtn').attr('data-tableId','patientPoolListBody');
    assignedDoctor();
});
//管理员  一键随机分配  和选择分配  和  操作栏分配按钮   end

//管理员 选择医生分配  弹出框  确认按钮   start
$('#selectDistributionSaveBtn').click(function(){
    var reservationIdArr=$(this).attr('data-reservationIdArr').split(',');
    for(var j=0;j<reservationIdArr.length;j++){
        reservationIdArr[j]=parseInt(reservationIdArr[j]);
    }
    var doctorIdInput=$(this).parent().prev().find('.selectDoctorBox input');
    var doctor_id;  //存储选择的医生的ID
    for(var j=0;j<doctorIdInput.length;j++){
        if($(doctorIdInput[j]).is(':checked')){
            doctor_id=$(doctorIdInput[j]).val();
        }
    }
    var result={ids:reservationIdArr,doctor_id:doctor_id};
    //todo 发信息部分  start
    // var sendMessageInput=$(this).parent().prev().find('.sendMessageType input');
    // result.send_message=[];
    // for(var m=0;m<sendMessageInput.length;m++){
    //     if($(sendMessageInput[m]).is(':checked')){
    //         result.send_message.push($(sendMessageInput[m]).val());
    //     }
    // }
    //todo 发信息部分  end
    $(this).prev().click();  //关闭模态框
    ajaxPost(RESERVATIONCENTERURL.patientPool.admin.assignTo, result, function(data){
        if(data.code == 10000){
            $('#patientPoolSearchBtn').click();
            alertModel('','分配成功！');
        }else if(data.code === 40100){
            aClick();
        }else {
            alertModel('','分配失败，请重试！');
        }
    }, function(){ });
});
//管理员 选择医生分配  弹出框  确认按钮   end




//用户  批量接受和拒绝   start
$('#doctorAcceptBtn').click(function(){
    var reservationIdArr = getReservationId('patientPoolListBody');
    if (reservationIdArr.length) {
        acceptRefused(RESERVATIONCENTERURL.patientPool.doctor.accept,'接受',reservationIdArr,'click');
    }else{
        alertModel('','请先选择患者！');
    }
});
$('#doctorRefusedBtn').click(function(){
    var reservationIdArr = getReservationId('patientPoolListBody');
    if (reservationIdArr.length) {
        acceptRefused(RESERVATIONCENTERURL.patientPool.doctor.reject,'拒绝',reservationIdArr,'click');
    }else{
        alertModel('','请先选择患者！');
    }
});
//用户  批量接受和拒绝   end
//用户  单个接受和拒绝   start
$('#patientPoolListBody').delegate('td button.accept','click',function(){
    var reservationId=new Array($(this).parent().parent().attr('data-reservationId'));
    acceptRefused(RESERVATIONCENTERURL.patientPool.doctor.accept,'接受',reservationId,$(this));
});
$('#patientPoolListBody').delegate('td button.refused','click',function(){
    var reservationId=new Array($(this).parent().parent().attr('data-reservationId'));
    acceptRefused(RESERVATIONCENTERURL.patientPool.doctor.reject,'拒绝',reservationId,$(this));
});
//用户  单个接受和拒绝   end

//接受 和  拒绝  方法  start
function acceptRefused(url,text,ids,status){
    confirmModel('','确定要'+text+'吗？','','',function(){
        ajaxPost(url, {ids:ids}, function(data){
            if(data.code == 10000){
                $('#patientPoolSearchBtn').click();
                alertModel('',text+'成功！');
            }else if(data.code === 40100){
                aClick();
            }else {
                alertModel('',text+'失败，请重试！');
            }
        }, function(){ })
    });
};
//接受 和  拒绝  方法  end